package com.example.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;

public class IniRealmTest {
    SimpleAccountRealm simpleAccountRealm = new SimpleAccountRealm();


    void testShiro(){
        //构建 SecurityManager
        DefaultSecurityManager securityManager = new DefaultSecurityManager();

        IniRealm realm = new IniRealm("classpath:user.ini");

        securityManager.setRealm(realm);

        //主体认证提交请求
        //用户传过来的数据   和reaml里面的进行匹配
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken("admin","123456");
        //认证
        subject.login(token);


        //授权
        subject.checkRole("user");

        subject.checkPermission("permit:delete");

        System.out.println("admin is login : " + subject.isAuthenticated());
        subject.logout();
        System.out.println("admin is login : " + subject.isAuthenticated());



    }

    public static void main(String[] args) {
        IniRealmTest test = new IniRealmTest();
        test.testShiro();
    }

}
